package crivia.db.jdbc;

public class DB {
	
	public enum Driver {
		
		Oracle(Driver_Oracle)
		,SQLServer(Driver_SQLServer)
		,MySQL(Driver_MySQL);
		
		public final String driver;
		Driver(String driver){
			this.driver = driver;
		}
		@Override
		public String toString() {
			return driver;
		}
		
	}
	
	private static final String Driver_Oracle = "oracle.jdbc.OracleDriver";

	private static final String Driver_SQLServer = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	
	private static final String Driver_MySQL = "com.mysql.jdbc.Driver";
	
	private String driver;
	
	private String url;
	
	private String name;
	
	private String username;
	
	private String password;

	public String getDriver() {
		return driver;
	}

	public void setDriver(String driver) {
		this.driver = driver;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		if(Driver_MySQL.equals(driver)){
			String urlMySQL=url;
			if(null!=url){
				if(url.indexOf("useOldAliasMetadataBehavior")<0){
					if(url.indexOf("?")<0){
						urlMySQL=url+"?useOldAliasMetadataBehavior=true";
					}else{
						urlMySQL=url+"&useOldAliasMetadataBehavior=true";
					}
				}
			}
			this.url = urlMySQL;
		}else{
			this.url = url;
		}
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public DB(Driver driver, String url, String name, String username, String password) {
		super();
		this.driver = driver.toString();
		this.name = name;
		this.username = username;
		this.password = password;
		if (url.startsWith("jdbc:")){
			this.url = url;
		} else {
			if (Driver_SQLServer.equals(this.driver)){
				this.url = "jdbc:sqlserver://" + url + ";DatabaseName=" + name;
			} else if (Driver_Oracle.equals(this.driver)) {
				this.url = "jdbc:oracle:thin:@" + url + ":" + name;
			} else {
				this.url = url;
			}
		}
	}

	public DB(String driver, String url, String name, String username, String password) {
		super();
		this.driver = driver;
		this.name = name;
		this.username = username;
		this.password = password;
		if (url.startsWith("jdbc:")){
			this.url = url;
		} else {
			if (Driver_SQLServer.equals(driver)){
				this.url = "jdbc:sqlserver://" + url + ";DatabaseName=" + name;
			} else if (Driver_Oracle.equals(driver)) {
				this.url = "jdbc:oracle:thin:@" + url + ":" + name;
			} else {
				this.url = url;
			}
		}
	}
	public DB(Driver driver, String url, String username, String password) {
		super();
		this.driver = driver.toString();
		setUrl(url);
		this.username = username;
		this.password = password;
	}
	public DB(String driver, String url, String username, String password) {
		super();
		this.driver = driver;
		setUrl(url);
		this.username = username;
		this.password = password;
	}
	

	public String type(){
		if (Driver_Oracle.equals(getDriver())){
			return crivia.db.common.SQL.DataBaseType_Oracle;
		} else if (Driver_SQLServer.equals(getDriver())){
			return crivia.db.common.SQL.DataBaseType_SQL_Server;
		} else if (Driver_MySQL.equals(getDriver())){
			return crivia.db.common.SQL.DataBaseType_MySQL;
		}
		return "";
	}

}
